package com.vibration.web.controller.baseinfo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.vibration.baseinfo.domain.TaskCountModel;
import com.vibration.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.vibration.common.annotation.Log;
import com.vibration.common.core.controller.BaseController;
import com.vibration.common.core.domain.AjaxResult;
import com.vibration.common.enums.BusinessType;
import com.vibration.baseinfo.domain.CzTemperatureData;
import com.vibration.baseinfo.service.ICzTemperatureDataService;
import com.vibration.common.utils.poi.ExcelUtil;
import com.vibration.common.core.page.TableDataInfo;

/**
 * 探针任务Controller
 * 
 * @author Mr_Wu
 * @date 2022-11-07
 */
@RestController
@RequestMapping("/baseinfo/temperatureData")
public class CzTemperatureDataController extends BaseController
{
    @Autowired
    private ICzTemperatureDataService czTemperatureDataService;


    /**
     * 根据任务id查询数据列表
     */
    @GetMapping(value = "/{taskId}")
    public AjaxResult getList(@PathVariable("taskId") Long taskId)
    {
        CzTemperatureData q = new CzTemperatureData();
        q.setTaskId(taskId);
        return AjaxResult.success(czTemperatureDataService.selectCzTemperatureDataList(q));
    }

    /**
     * 查询探针任务列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CzTemperatureData czTemperatureData) throws ParseException {

        //处理时间为时间戳
        Map<String, Object> params = czTemperatureData.getParams();
        if(params != null){
            Object b =  params.get("beginTime");
            Object e = params.get("endTime");
            if(b!=null && e!=null){
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                long st = format.parse(b.toString()).getTime();
                long et = format.parse(e.toString()).getTime();
                params.put("beginTime",st);
                params.put("endTime",et);
            }
        }
        startPage();
        List<CzTemperatureData> list = czTemperatureDataService.selectCzTemperatureDataList(czTemperatureData);
        return getDataTable(list);
    }

    /**
     * 导出探针任务列表
     */
    @Log(title = "探针任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CzTemperatureData czTemperatureData) throws ParseException {
        //处理时间为时间戳
        Map<String, Object> params = czTemperatureData.getParams();
        if(params != null){
            Object b =  params.get("beginTime");
            Object e = params.get("endTime");
            if(b!=null && e!=null){
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                long st = format.parse(b.toString()).getTime();
                long et = format.parse(e.toString()).getTime();
                params.put("beginTime",st);
                params.put("endTime",et);
            }
        }
        List<CzTemperatureData> list = czTemperatureDataService.selectCzTemperatureDataList(czTemperatureData);
        //处理时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        for (CzTemperatureData data : list) {
            if(data.getDataTime()!=null){
                data.setDataTimeStr1(format.format(new Date(data.getDataTime())));
            }
        }
        ExcelUtil<CzTemperatureData> util = new ExcelUtil<CzTemperatureData>(CzTemperatureData.class);
        util.exportExcel(response, list, "探针任务数据");
    }

    /**
     * 获取探针任务详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(czTemperatureDataService.selectCzTemperatureDataById(id));
    }

    /**
     * 新增探针任务
     */
    @Log(title = "探针任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CzTemperatureData czTemperatureData)
    {
        return toAjax(czTemperatureDataService.insertCzTemperatureData(czTemperatureData));
    }

    /**
     * 修改探针任务
     */
    @Log(title = "探针任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CzTemperatureData czTemperatureData)
    {
        return toAjax(czTemperatureDataService.updateCzTemperatureData(czTemperatureData));
    }

    /**
     * 删除探针任务
     */
    @Log(title = "探针任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(czTemperatureDataService.deleteCzTemperatureDataByIds(ids));
    }

    /***
     * 温度统计
     */
    @GetMapping(value = "/temperatureChange")
    public AjaxResult taskCount ( String equipmentCode)
    {
        //震动任务
        List<CzTemperatureData> wdbh = czTemperatureDataService.selectCzTemperatureDataByCodeOneHour(equipmentCode);
        AjaxResult result = AjaxResult.success();
        result.put("wdbh",wdbh);
        return result;
    }

}
